package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectServiceDuration;
import com.shrct.businessprocess.project.RCProjectType;
import com.shrct.businessprocess.project.impl.RCProjectTypeExtensionImpl;
import com.shrct.businessprocess.project.proxy.RCProjectServiceDurationProxy;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCPROJECTSERVICEDURATION_TYPE")
public class RCProjectServiceDurationImpl extends RCProjectTypeExtensionImpl implements RCProjectServiceDuration {
  public RCProjectServiceDurationImpl() {
    super();
  }
  
  private Date startDate;
  
  public Date getStartDate() {
    return this.startDate;
  }
  
  public RCProjectServiceDuration setStartDate(final Date startDate) {
    this.startDate = startDate;
    return this;			
    
  }
  
  private Date endDate;
  
  public Date getEndDate() {
    return this.endDate;
  }
  
  public RCProjectServiceDuration setEndDate(final Date endDate) {
    this.endDate = endDate;
    return this;			
    
  }
  
  public void initType() {
    this.setType(RCProjectType.serviceDuration);
  }
  
  public Double calcPercent() {
    double _xblockexpression = (double) 0;
    {
      boolean _or = false;
      boolean _equals = Objects.equal(this.startDate, null);
      if (_equals) {
        _or = true;
      } else {
        boolean _equals_1 = Objects.equal(this.endDate, null);
        _or = (_equals || _equals_1);
      }
      if (_or) {
        return Double.valueOf(0d);
      }
      Date _day = DateUtil.today();
      Long _between = DateUtil.between(this.startDate, _day);
      double _max = Math.max(0d, (_between).longValue());
      Long _between_1 = DateUtil.between(this.startDate, this.endDate);
      double _divide = (_max / (_between_1).longValue());
      double _min = Math.min(_divide, 1d);
      _xblockexpression = (_min);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public Double calcTotalIncome() {
    RCProject _project = this.getProject();
    RCProjectMoney _totalContractAmount = _project.getTotalContractAmount();
    Double _hostAmount = _totalContractAmount.toHostAmount();
    Double _calcPercent = this.calcPercent();
    double _multiply = DoubleExtensions.operator_multiply(_hostAmount, _calcPercent);
    return Double.valueOf(_multiply);
  }
  
  public static RCProjectServiceDuration create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCProjectServiceDuration rCProjectServiceDuration = new com.shrct.businessprocess.project.impl.RCProjectServiceDurationImpl(
    );
    objectFactory.create(rCProjectServiceDuration);
    return rCProjectServiceDuration;			
    
  }
  
  public static RCProjectServiceDuration createRCProjectServiceDurationImpl(final IObjectFactory objectFactory, final Procedure1<RCProjectServiceDuration> init) {
    com.shrct.businessprocess.project.RCProjectServiceDuration rCProjectServiceDuration = new com.shrct.businessprocess.project.impl.RCProjectServiceDurationImpl(
    );
    objectFactory.create(rCProjectServiceDuration);
    init.apply(rCProjectServiceDuration);
    
    return rCProjectServiceDuration;			
    
  }
  
  public RCProjectServiceDurationProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCProjectServiceDurationProxy proxy = new com.shrct.businessprocess.project.proxy.RCProjectServiceDurationProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
